package root.electronclass.electronclass.home.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import root.electronclass.electronclass.common.util.TextUtils;
import root.electronclass.electronclass.mvp.GlobalParameter;
import root.electronclass.electronclass.mvp.base.BaseResponse;
import root.electronclass.electronclass.mvp.entity.HomeImgInfoH;
import root.electronclass.electronclass.mvp.rest.RestManager;
import root.electronclass.electronclass.MainActivity;
import root.electronclass.electronclass.R;
import root.electronclass.electronclass.databinding.HomeImgInfoBinding;
import root.electronclass.electronclass.grade.record.ClassRecordFragment;
import root.electronclass.electronclass.other.PictureActivity;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HomeImgView {
    private static final String TAG = "HomeImgView";

    private Context context;
    private HomeImgInfoBinding binding;

    private int index = 0;

    public HomeImgView(Context context, ViewGroup view) {
        this.binding = HomeImgInfoBinding.bind(view);
        this.context = context;
        init();
    }

    public void refresh() {
        request();
    }

    private void init() {
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity fragmentActivity = ((MainActivity)context);
                Bundle bundle = new Bundle();
                bundle.putSerializable("TARGET_CLASS", ClassRecordFragment.class);
                fragmentActivity.selectPage(MainActivity.Page.CLASS, bundle);
            }
        });
        request();
    }

    private void initViewPager(String[] pics, String title) {
        if (pics == null) {
            return;
        }
        index = 0;
        List<View> mViews = new ArrayList<>();
        ArrayList<String> picList = new ArrayList<>();
        for (int i = 0; i < pics.length; i++) {
            picList.add(TextUtils.checkAndGetPicUrl(pics[i]));
        }
        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.home_item_img, null, false);
            TextView tvTitle = view.findViewById(R.id.tvTitle);
            SimpleDraweeView pic = view.findViewById(R.id.imgPic);
            tvTitle.setText(title);
            pic.setImageURI(pics[i]);
            final int tempIndex = i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    PictureActivity.startActivity(context, picList, tempIndex);
                }
            });
            mViews.add(view);
        }

        PagerAdapter adapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return mViews == null ? 0 : mViews.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView(mViews.get(position));
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                container.addView(mViews.get(position));
                return mViews.get(position);
            }
        };

        binding.viewPager.setAdapter(adapter);

        binding.layoutNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index < mViews.size() - 1) {
                    index++;
                    binding.viewPager.setCurrentItem(index);
                }
            }
        });

        binding.layoutPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index > 0) {
                    index--;
                    binding.viewPager.setCurrentItem(index);
                }
            }
        });

        binding.viewPager.setCurrentItem(0);
    }




    public void request() {
        long id = GlobalParameter.getEcardInfo().getClassId();
        RestManager.getInstance().initRest().imgList(String.valueOf(id)).enqueue(new Callback<BaseResponse<HomeImgInfoH>>() {
            @Override
            public void onResponse(Call<BaseResponse<HomeImgInfoH>> call, Response<BaseResponse<HomeImgInfoH>> response) {
                if (response.isSuccessful()) {
                    HomeImgInfoH homeImgInfoH = response.body().getRows();
                    if (homeImgInfoH != null && homeImgInfoH.getPicList() != null) {
                        String[] pics = new String[homeImgInfoH.getPicList().size()];
                        for (int i = 0; i < homeImgInfoH.getPicList().size(); i++) {
                            pics[i] = homeImgInfoH.getPicList().get(i).getUrl();
                        }
                        initViewPager(pics, homeImgInfoH.getTitle());
                    }

                }
            }

            @Override
            public void onFailure(Call<BaseResponse<HomeImgInfoH>> call, Throwable t) {

            }
        });
    }


}
